# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import csv

class ScrapyZuqiucaipiaoDomePipeline:
    def open_spider(self, spider):
        self.file = open('data1.csv', 'w', newline='', encoding='utf-8')
        print("文件打开成功")
        self.file.write('周几,比赛名,比赛轮次,比赛时间,比赛状态,主队,比分,客队,进球\n')
        return spider
    # fieldnames = ['weekday', 'name', 'round_and_grouping', 'time', 'status', 'zhudui', 'score', 'kedui', 'jinqiu']
    def process_item(self, item, spider):
        self.file.write(item['weekday']+','+item['name']+','+item['round_and_grouping']+','+item['time']+','+item['status']+','+item['zhudui']+','+item['score']+','+item['kedui']+','+item['jinqiu']+'\n')
        print("写入成功")
        return item
    def close_spider(self, spider):
        self.file.close()

import pymysql
class MysqlPipeline:
    def open_spider(self, spider):
        self.conn=pymysql.connect(
            host="localhost",
            port=3306,user="root",
            password="17884873770",
            database="spider01",
            charset="utf8")
        self.cursor=self.conn.cursor()
        print("数据库连接成功")

    def process_item(self, item, spider):
        jinqiu=item['jinqiu']
        kedui=item['kedui']
        name=item['name']
        round_and_grouping=item['round_and_grouping']
        score=item['score']
        status=item['status']
        time=item['time']
        weekday=item['weekday']
        zhudui=item['zhudui']
        print("数据写入数据库成功")
        sql=('insert into data(name,round_and_grouping,weekday,status,time,zhudui,score,kedui,jinqiu) '
             'values("{}","{}","{}","{}","{}","{}","{}","{}","{}")').format(name,round_and_grouping,weekday,status,time,zhudui,score,kedui,jinqiu)
        self.cursor.execute(sql)
        self.conn.commit()
        return item
    def close_spider(self,spider):
        self.cursor.close()
        self.conn.close()